﻿using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// Implements the interface <see cref="IModuleSettingsProvider"/> for a given ModuleInfo.
    /// </summary>
    public class ModuleSettingsProvider : IModuleSettingsProvider
    {
        #region fields

        private readonly ModuleInfo module;

        #endregion

        #region properties

        private ModuleController moduleController;

        private ModuleController ModuleController
        {
            get
            {
                if (null == this.moduleController)
                {
                    this.moduleController = new ModuleController();
                }
                return this.moduleController;
            }
        }

        #endregion

        #region constructors

        /// <summary>
        /// Creates a new ModuleSettingsProvider for the given <see cref="ModuleInfo"/>.
        /// </summary>
        /// <param name="module">The <see cref="ModuleInfo"/> for which to create the ModuleSettingsProvider.</param>
        public ModuleSettingsProvider(ModuleInfo module)
        {
            if (null == module)
                throw new ArgumentNullException("module");

            this.module = module;
        }

        #endregion

        #region IModuleSettingsProvider Members

        /// <summary>
        /// Gets the module's configuration data.
        /// </summary>
        public ModuleInfo ModuleConfiguration
        {
            get { return module; }
        }

        /// <summary>
        /// Gets the module's settings.
        /// </summary>
        public System.Collections.Hashtable Settings
        {
            get
            {
                return PortalSettings.GetTabModuleSettings(
                    this.ModuleController.GetModuleSettings(module.ModuleID),
                    this.ModuleController.GetTabModuleSettings(module.TabModuleID));
            }
        }

        #endregion
    }
}
